package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.patrons.CombInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.config.skin.SkinPatronsSkill;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.eCombType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.enums.eSkinType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkinBond;
import com.yanqu.road.entity.player.UserSkin;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.bussiness.config.PatronsBussiness;
import com.yanqu.road.logic.bussiness.config.SkinConfigBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.logic.resource.GoodsResourceLogic;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.ConfigHelper;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/9/1
 */
@ConfigMgrMark
public class SkinMgr extends TempMgr {

    private static Map<Integer, SkinInfo> skinInfoMap;

    private static Map<Integer, CombInfo> patronsSkinCombMap;

    private static Map<Integer, Map<Integer, SkinInfo>> assistantPartDecorationMap;

    private static Map<Integer, CombInfo> assistantSkinCombMap;
    //key:combId
    private static Map<Integer, CombInfo> patronsSkinBondMap;
    //key:skinId
    private static Map<Integer, CombInfo> patronsSkinBondBySkinIdMap;
    //key:patronsId
    private static Map<Integer, Map<Integer, SkinInfo>> patronsSkinInfoMap;

    public static SkinInfo getSkinInfo(int skinId) {
        return skinInfoMap.get(skinId);
    }

    public static Map<Integer, SkinInfo> getSkinInfoMap() {
        return skinInfoMap;
    }

    public static Map<Integer, SkinInfo> getPatronsSkinInfo(int patronsId) {
        return patronsSkinInfoMap.getOrDefault(patronsId, new HashMap<>());
    }

    public static List<Integer> getSkinListByRoleId(eSkinType skinType, int belongId) {
        List<Integer> list = new ArrayList<>();
        for (SkinInfo skinInfo : skinInfoMap.values()) {
            if (skinInfo.getType() == skinType.getValue() && skinInfo.getBelongRole() == belongId) {
                list.add(skinInfo.getSkinId());
            }
        }
        return list;
    }

    /**
     * 获取门客皮肤组合
     * @param combId
     * @return
     */
    public static CombInfo getPatronsSkinComb(int combId){
        return patronsSkinCombMap.get(combId);
    }

    public static List<Integer> getSkinListByComb(int combId){
        List<Integer> skinIdList = new ArrayList<>();
        for (Map.Entry<Integer, SkinInfo> infoEntry : skinInfoMap.entrySet()) {
            if(infoEntry.getValue().getCombId() == combId){
                skinIdList.add(infoEntry.getValue().getSkinId());
            }
        }
        return skinIdList;
    }

    public static int getLinkType(int skinId,long getTime) {
        SkinInfo skinInfo = getSkinInfo(skinId);
        if (skinInfo != null) {
            long linkAge = skinInfo.getLinkAge();
            if (getTime < linkAge) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 初始化门客组合信息
     */
    private void initPatronsSkinComb(Map<Integer, CombInfo> combMap){
        for(CombInfo combInfo : combMap.values()){
            List<Integer> objectIdList = new ArrayList<>();
            for(SkinInfo skinInfo : skinInfoMap.values()){
                if(combInfo.getId() == skinInfo.getCombId()){
                    if(!objectIdList.contains(skinInfo.getSkinId())){
                        objectIdList.add(skinInfo.getSkinId());
                    }
                }
            }
            combInfo.setObjectIdList(objectIdList);
        }
    }

    public static List<Integer> getCombCombObjectIdList(Integer combId){
        List<Integer> combVehicleIdList = new ArrayList<>();
        if(!patronsSkinCombMap.containsKey(combId)){
            return combVehicleIdList;
        }
        CombInfo combInfo = patronsSkinCombMap.get(combId);
        combVehicleIdList.addAll(combInfo.getObjectIdList());
        for(Integer combCombId : combInfo.getCombList()){
            if(patronsSkinCombMap.containsKey(combCombId)){
                combVehicleIdList.addAll(patronsSkinCombMap.get(combCombId).getObjectIdList());
            }
        }
        return combVehicleIdList;
    }

    /**
     * 门客组合是否解锁
     */
    public static boolean isUnlockPatronsSkinComb(int skinId, Map<Integer, UserSkin> userSkinMap){
        SkinInfo skinInfo = getSkinInfo(skinId);
        if(null == skinInfo){
            return false;
        }
        if(skinInfo.getCombId() <= 0){
            return false;
        }
        if(null == userSkinMap){
            return false;
        }
        CombInfo patronsSkinComb = getPatronsSkinComb(skinInfo.getCombId());
        if(null == patronsSkinComb){
            return false;
        }
        if(null == patronsSkinComb.getObjectIdList() || patronsSkinComb.getObjectIdList().size() <= 0){
            return false;
        }
        for(int tempSkinId : patronsSkinComb.getObjectIdList()){
            if(!userSkinMap.containsKey(tempSkinId)){
                return false;
            }
        }
        return true;
    }

    /**
     * 门客组合是否解锁
     */
    public static SkinInfo getSkinInfo(int skillId, int patronsId){
        CombInfo patronsSkinComb = null;
        for(CombInfo combInfo : patronsSkinCombMap.values()){
            if(combInfo.getSkillId(0) == skillId){
                patronsSkinComb = combInfo;
                break;
            }
        }
        if(null == patronsSkinComb){
            return null;
        }
        for(SkinInfo skinInfo : skinInfoMap.values()){
            if(skinInfo.getType() == eSkinType.Patrons.getValue() && skinInfo.getCombId() == patronsSkinComb.getId() &&
                skinInfo.getBelongRole() == patronsId){
                return skinInfo;
            }
        }
        return null;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        skinInfoMap = SkinConfigBusiness.getSkinInfoMap();
        patronsSkinInfoMap = parsePatronsSkinInfoMap(skinInfoMap);
        Map<Integer, CombInfo> tempCombMap = PatronsBussiness.getPatronsCombMap(eCombType.PatronsSkin.getValue());
        initPatronsSkinComb(tempCombMap);
        patronsSkinCombMap = tempCombMap;
        initAssistantPartDecorationMap(skinInfoMap);    //小助理部位皮肤初始化
        Map<Integer, CombInfo> tempAssistantCombMap = PatronsBussiness.getPatronsCombMap(eCombType.assistantSkin.getValue());//拿出小助理的组合
        initPatronsSkinComb(tempAssistantCombMap);          //和门客组合同一个方法初始化
        assistantSkinCombMap = tempAssistantCombMap;
        Map<Integer, CombInfo> tempskinBondMap = PatronsBussiness.getPatronsCombMap(eCombType.PatronsSkinBond.getValue());
        patronsSkinBondMap = tempskinBondMap;
        patronsSkinBondBySkinIdMap = parsePatronsSkinBondMapToSkinIdMap(tempskinBondMap);
        // return !isPatronsSkillRepeat(skinInfoMap);
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static String getSkinResourceFlag(int skinId) {
        SkinInfo skinInfo = getSkinInfo(skinId);
        return GoodsResourceLogic.getFlag(skinInfo.getIcon(), skinInfo.getSound(), skinInfo.getAppearanceId());
    }

    public static String getSkinTagFlag(int skinId) {
        SkinInfo skinInfo = getSkinInfo(skinId);
        return skinInfo.getTag();
    }

    public static List<Integer> getPatronsLTPMainRoleSkinList(int patronsId) {
        PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(patronsId);
        int occupation = patronsInfo == null ? 0 : patronsInfo.getOccupation();

        List<Integer> skinList = new ArrayList<>();
        for (SkinInfo skinInfo : skinInfoMap.values()) {
            int addiOccu = changeToPatronsOccupation(skinInfo.getBelongRole());
            if (skinInfo.getBelongRole() == patronsId || addiOccu == occupation || addiOccu == 0) {
                skinList.add(skinInfo.getSkinId());
            }
        }
        return skinList;
    }

    /**
     * 特殊belongRole转换为门客职业
     *
     * @return -1表示转化不来，0表示全部
     */
    public static int changeToPatronsOccupation(int specialBelongRole) {
        if (specialBelongRole == -100) { // 全体
            return 0;
        }
        if (specialBelongRole == -101) { // 士
            return 1;
        }
        if (specialBelongRole == -102) { // 农
            return 2;
        }
        if (specialBelongRole == -103) { // 工
            return 3;
        }
        if (specialBelongRole == -104) { // 商
            return 4;
        }
        if (specialBelongRole == -105) { // 侠
            return 5;
        }
        return -1;
    }

    public static boolean isLTPMainRoleSkin(int skinId) {
        SkinInfo skinInfo = skinInfoMap.get(skinId);
        if (skinInfo != null) {
            return skinInfo.getType() == eSkinType.LTPMainRole.getValue();
        }
        return false;
    }

    /**
     * 初始化小助理部位皮肤map
     */
    private void initAssistantPartDecorationMap(Map<Integer, SkinInfo> skinInfoMap){
        //以部位区分小助手皮肤
        Map<Integer, Map<Integer, SkinInfo>> tmpAssistantPartDecoration = new ConcurrentHashMap<>();
        Map<Integer, SkinInfo> copySkinInfoMap = new ConcurrentHashMap<>(skinInfoMap);
        for (SkinInfo skinInfo : copySkinInfoMap.values()) {
            if (skinInfo.getType() == eSkinType.Assistant.getValue()) {
                if (tmpAssistantPartDecoration.get(skinInfo.getBelongRole()) == null) {
                    tmpAssistantPartDecoration.put(skinInfo.getBelongRole(), new ConcurrentHashMap<Integer, SkinInfo>());
                }
                tmpAssistantPartDecoration.get(skinInfo.getBelongRole()).put(skinInfo.getSkinId(), skinInfo);
            }
        }
        assistantPartDecorationMap = tmpAssistantPartDecoration;
    }

    /**
     * 通过技能编号获取技能升级类型
     * @param skillId
     * @return
     */
    public static SkinPatronsSkill getSkinPatronsSkillUpgradeType(int skillId){
        //有双生的配置，用双生的
        for(SkinInfo skinInfo : skinInfoMap.values()){
            if(skinInfo.getType() == eSkinType.Patrons.getValue() && skinInfo.getTwinsId() > 0){
                for(SkinPatronsSkill skinPatronsSkill : skinInfo.getSkinPatronsSkillList()){
                    if(skinPatronsSkill.getSkillId() == skillId){
                        return skinPatronsSkill;
                    }
                }
            }
        }
        //没有双生配置
        for(SkinInfo skinInfo : skinInfoMap.values()){
            if(skinInfo.getType() == eSkinType.Patrons.getValue()){
                for(SkinPatronsSkill skinPatronsSkill : skinInfo.getSkinPatronsSkillList()){
                    if(skinPatronsSkill.getSkillId() == skillId){
                        return skinPatronsSkill;
                    }
                }
            }
        }
        return null;
    }

    public static Map<Integer, Map<Integer, SkinInfo>> getAssistantPartDecorationMap() {
        return assistantPartDecorationMap;
    }

    public static Map<Integer, CombInfo> getAssistantSkinCombMap() {
        return assistantSkinCombMap;
    }

    /**
    * 描述：获取双生皮肤信息
    * 作者：zrq
    * 时间：2021/12/31
    * 参数：
    皮肤编号
    * 返回值：双生皮肤信息
    **/
    public static SkinInfo getTwinsSkinInfo(int skinId){
        SkinInfo skinInfo = getSkinInfo(skinId);
        if(null == skinInfo){
            return null;
        }
        SkinInfo twinsSkinInfo = null;
        if(skinInfo.getTwinsId() > 0) {
            twinsSkinInfo = getSkinInfo(skinInfo.getTwinsId());
        }
        return twinsSkinInfo;
    }

    /**
    * 描述：是否府邸皮肤
    * 作者：zrq
    * 时间：2022/1/21
    * 参数：
    皮肤编号
    * 返回值：是否府邸皮肤
    **/
    public static boolean isEstateSkin(int skinId) {
        SkinInfo skinInfo = skinInfoMap.get(skinId);
        if (null != skinInfo) {
            return skinInfo.getType() == eSkinType.EstateSkin.getValue();
        }
        return false;
    }

    public static boolean isBankSkin(int skinId) {
        SkinInfo skinInfo = skinInfoMap.get(skinId);
        if (null != skinInfo) {
            return skinInfo.getType() == eSkinType.BankSkin.getValue();
        }
        return false;
    }

    private static Map<Integer, CombInfo> parsePatronsSkinBondMapToSkinIdMap(Map<Integer, CombInfo> combInfoMap) {
        Map<Integer, CombInfo> skinIdMap = new HashMap<>();
        for (CombInfo combInfo : combInfoMap.values()) {
            for (Integer skinId : combInfo.getCombList()) {
                if (skinIdMap.containsKey(skinId)) {
                    //不允许同个皮肤id存在多个羁绊内
//                    throw new RuntimeException(String.format("不允许同个皮肤id=%d存在多个羁绊内,已在羁绊%d内,当前羁绊%d", skinId, skinIdMap.get(skinId).getId(), combInfo.getId()));
                    getLogger().error("!!!!!不允许同个皮肤id={}存在多个羁绊内,已在羁绊{}内,当前羁绊{}!!!!!", skinId, skinIdMap.get(skinId).getId(), combInfo.getId());
                }
                skinIdMap.put(skinId, combInfo);
            }
        }
        return skinIdMap;
    }

    private static Map<Integer, Map<Integer, SkinInfo>> parsePatronsSkinInfoMap(Map<Integer, SkinInfo> skinInfoMap) {
        Map<Integer, Map<Integer, SkinInfo>> patronsIdMap = new HashMap<>();
        for (SkinInfo skinInfo : skinInfoMap.values()) {
            if (skinInfo.getType() != eSkinType.Patrons.getValue()) {
                continue;
            }
            int patronsId = skinInfo.getBelongRole();
            Map<Integer, SkinInfo> skinIdMap = patronsIdMap.get(patronsId);
            if (null == skinIdMap) {
                skinIdMap = new HashMap<>();
                patronsIdMap.put(patronsId, skinIdMap);
            }
            skinIdMap.put(skinInfo.getSkinId(), skinInfo);
        }
        return patronsIdMap;
    }

    public static CombInfo getSkinBondCombInfo(int combId) {
        return patronsSkinBondMap.get(combId);
    }

    public static CombInfo getSkinBondCombInfoBySkinId(int skinId) {
        return patronsSkinBondBySkinIdMap.get(skinId);
    }

    /**
     * 皮肤羁绊加的资质
     */
    public static int getPatronsSkinBondAddQualification(int patronsId, Map<Integer, UserPatronsSkinBond> userPatronsSkinBondMap, Map<Integer, UserSkin> userSkinMap) {
        int addValue = 0;
        Map<Integer, SkinInfo> patronsSkinInfo = SkinMgr.getPatronsSkinInfo(patronsId);
        Set<CombInfo> bondCombSet = new HashSet<>();
        for (SkinInfo skinInfo : patronsSkinInfo.values()) {
            if (!userSkinMap.containsKey(skinInfo.getSkinId())){
                continue;
            }
            CombInfo skinBondCombInfo = SkinMgr.getSkinBondCombInfoBySkinId(skinInfo.getSkinId());
            if (null == skinBondCombInfo) {
                continue;
            }
            UserPatronsSkinBond userPatronsSkinBond = userPatronsSkinBondMap.get(skinBondCombInfo.getId());
            if (null == userPatronsSkinBond) {
                continue;
            }

            for (Integer skinId : userPatronsSkinBond.getBondIdSet()) {
                if (userSkinMap.containsKey(skinId)){
                    bondCombSet.add(skinBondCombInfo);
                    break;
                }
            }
        }

        for (CombInfo skinBondCombInfo : bondCombSet) {
            UserPatronsSkinBond userPatronsSkinBond = userPatronsSkinBondMap.get(skinBondCombInfo.getId());
            if (null == userPatronsSkinBond) {
                continue;
            }
            for (Integer skinId : userPatronsSkinBond.getBondIdSet()) {
                int skillId = skinBondCombInfo.getSkillId(skinId);
                SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
                if (null == skillInfo) {
                    continue;
                }
                if (skillInfo.getType() != eSkillType.SkinBondAddQualification.getValue()){
                    continue;
                }
                UserSkin userSkin = userSkinMap.get(skinId);
                if (null == userSkin) {
                    continue;
                }
                int skinAdd = skillInfo.getParamList().get(0);
                skinAdd += (userSkin.getLevel() - 1) * skillInfo.getUpgradeParamList().get(0);

                addValue += skinAdd;
            }
        }

        return addValue;
    }

    /**
     * 羁绊光环增加的赚钱千分比
     *
     * @return
     */
    public static int getPatronsSkinBondCombAddValue(int patronsId, Map<Integer, UserPatronsSkinBond> userPatronsSkinBondMap, Map<Integer, UserSkin> userSkinMap) {
        int addValue = 0;
        Map<Integer, SkinInfo> patronsSkinInfo = SkinMgr.getPatronsSkinInfo(patronsId);
        Set<CombInfo> bondCombSet = new HashSet<>();
        for (SkinInfo skinInfo : patronsSkinInfo.values()) {
            if (!userSkinMap.containsKey(skinInfo.getSkinId())) {
                continue;
            }
            CombInfo skinBondCombInfo = SkinMgr.getSkinBondCombInfoBySkinId(skinInfo.getSkinId());
            if (null == skinBondCombInfo) {
                continue;
            }
            UserPatronsSkinBond userPatronsSkinBond = userPatronsSkinBondMap.get(skinBondCombInfo.getId());
            if (null == userPatronsSkinBond) {
                continue;
            }
            if (userPatronsSkinBond.getLevel() == 0) {
                continue;
            }
            for (Integer skinId : userPatronsSkinBond.getBondIdSet()) {
                if (userSkinMap.containsKey(skinId)){
                    bondCombSet.add(skinBondCombInfo);
                    break;
                }
            }
        }

        for (CombInfo skinBondCombInfo : bondCombSet) {
            UserPatronsSkinBond userPatronsSkinBond = userPatronsSkinBondMap.get(skinBondCombInfo.getId());
            if (null == userPatronsSkinBond) {
                continue;
            }
            SkillInfo skillInfo = SkillMgr.getSkillInfo(skinBondCombInfo.getCombSkillId());
            if (null == skillInfo) {
                continue;
            }

            if (skillInfo.getType() != eSkillType.SkinBondCombAddition.getValue()){
                continue;
            }
            int combAddValue = skillInfo.getParamList().get(1);
            combAddValue += (userPatronsSkinBond.getLevel() - 1) * skillInfo.getUpgradeParamList().get(1);

            addValue += combAddValue;
        }

        return addValue;
    }

    /**
     * 美女皮肤排行ID转换
     * @param skinId
     * @return
     */
    public static int rankBeautySkinIdTransform(int skinId) {
        SkinInfo skinInfo = SkinMgr.getSkinInfo(skinId);

        if (eSkinType.Beauty.getValue() == skinInfo.getType()) {

            //大陆美女2合1转换，女绣娘，男绣娘，排行ID用iconId
            if(ConfigMgr.isBeautySetOne(skinInfo.getBelongRole())){
                long serverId = ConfigHelper.getLong("serverId");
                ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
                int channelId = 0;
                if(serverInfo != null){
                    channelId = serverInfo.getChannelId();
                }
                if(eChannelType.isMainLandChannel(channelId)){
                    //女绣娘，男绣娘，排行ID用 iconId
                    return Integer.parseInt(skinInfo.getIcon());
                }
            }

            //其他转换。。。。
        }
        return skinId;
    }
}
